server_name=$1
tag=$2
port=$3
active=$4

harbor_url="nrpcloud.tencentcloudcr.com"
imagename=$harbor_url/pet-medical-cloud-$active/$server_name:$tag

containerid=$(docker ps -a | grep $server_name | awk '{print $1}' | sed 's/%//g')
if [ $containerid ]; then
  echo "容器存在，即将删除"
	docker stop $containerid
	docker rm $containerid
else
	echo $server_name is null
fi

docker login --username=100026707997 -p eyJhbGciOiJSUzI1NiIsImtpZCI6IkpHRFc6SlNMWDo2QTdGOjZVTlM6SEVZTjpENEJMOk1BSVo6TjZVSTozSFE0Okc1UVE6NzRLRTpZU0ZKIn0.eyJvd25lclVpbiI6IjEwMDAyNjQyNjg1MSIsIm9wZXJhdG9yVWluIjoiMTAwMDI2NzA3OTk3IiwidG9rZW5JZCI6ImNkbGhvOWRlN2NkYWwxbmpvajIwIiwiZXhwIjoxOTgzMzIzOTQxLCJuYmYiOjE2Njc5NjM5NDEsImlhdCI6MTY2Nzk2Mzk0MX0.SFiSJWNb_ZN_tNr6ahwqu1iMixx5YFqQy6_2SqbQzQ5RLsbl8ayq8eJZV2k1DstbmbG5NTnnxhLXBQ_cP67kSZ61T5Jao42me5tKvlQyqtu9sm-xW3sLt3EZW6l8zEDwfWsqE1-l2hf7woFx23y-Ckw9IwAxCEBI-fKGcsw9W71b-WPi9z2WjHkd2QgYLpHcuzwXJKVBK1OpoWCJQKDT1-6vgpUhXBP3V1aspEBkPRZA36jS6mrpiTOR6wKjM-k5o5aXzjnoAf3G2SXbzMvaKz3ea2DSxBSzdyXcr3iEN0zaP5UUkZNp1pl4hdJBNkXNhOkHzEOHmPwmsyL3MLRiuw $harbor_url
docker pull $imagename

docker run --restart=on-failure:3 -d --name $server_name  \
--network=host \
-e SPRING_PROFILES_ACTIVE=$active \
-e SERVER_NAME=$server_name-server \
-v /home/data/:/home/data  \
-p $port:$port \
$imagename

echo "容器启动成功"

##判断镜像是否存在
image_id=`docker images | grep $server_name | awk '{print$3}'`
if [ $image_id] != "" ];then
  echo "删除镜像"
  docker rmi $imagename
fi
# 注意权限 chmod 777 deploy.sh
